<?php

namespace App\Models;

use App\Util\Tool;
use Illuminate\Database\Eloquent\Model;

class HealthReminder extends Model
{
    protected $table = 'c_health_reminders';

    public function setTakeTimeAttribute($gallery)
    {
        if (is_array($gallery)) {
            $this->attributes['take_time'] = json_encode($gallery);
        }
    }

    public function getTakeTimeAttribute($gallery)
    {
        return json_decode($gallery, true);
    }


    public function getUseTypes($data)
    {
        //注释	1每天2每两天3每3天4每周
        switch ($data['use_type']) {
            case 1:
                return "每天";
                break;
            case 2:
                return "每2天";
                break;
            case 3:
                return "每3天";
                break;
            case 4:
                return "每周";
                break;
        }
    }

    public function getFrequencyDec($data)
    {
        //注释	注释	用药频次1:一天一次2:一天两次,3一天3次,4两天一次,5两天两次6两天三次
        switch ($data['frequency']) {
            case 1:
                return "1次";
                break;
            case 2:
                return "2次";
                break;
            case 3:
                return "3次";
                break;
            case 4:
                return "4次";
                break;
            case 5:
                return "5次";
                break;
            case 6:
                return "6次";
                break;
            case 7:
                return "7次";
                break;
            case 8:
                return "8次";
                break;
        }
    }


    /****
     * @param $data
     * @return bool
     * 添加健康提醒
     */
    public function addHealthReminder($data)
    {
        if (isset($data['id']) && !empty($data['id'])) {
            $redata = $this->getEditor($data['id']);
            $redata->user_id = isset($data['user_id']) ? $data['user_id'] : $redata->user_id;
            $redata->case_id = isset($data['case_id']) ? $data['case_id'] : $redata->case_id;
            $redata->use_type = isset($data['use_type']) ? $data['use_type'] : $redata->use_type;
            $redata->frequency = isset($data['frequency']) ? $data['frequency'] : $redata->frequency;
            $redata->name = isset($data['name']) ? $data['name'] : $redata->name;
            $redata->take_time = isset($data['take_time']) ? Tool::handelString($data['take_time']) : $redata->take_time;
            $redata->remark = isset($data['remark']) ? $data['remark'] : '';
            $redata->status = isset($data['status']) ? $data['status'] : $redata->status;
            $redata->meal_type = isset($data['meal_type']) ? $data['meal_type'] : $redata->meal_type;
            $redata->start_time = isset($data['start_time']) ? $data['start_time'] : $redata->start_time;
            $redata->end_time = isset($data['end_time']) ? $data['end_time'] : $redata->end_time;
            $res = $redata->save();
        } else {
            //添加
            $this->user_id = $data['user_id'];
            $this->case_id = isset($data['case_id'])?$data['case_id']:0;
            $this->start_time = !empty($data['start_time'])?$data['start_time']:date('Y-m-d', time());
            $this->end_time = !empty($data['end_time'])?$data['end_time']:date('Y-m-d', time()); //$data['end_time'];
            $this->use_type = $data['use_type'];
            $this->frequency = $data['frequency'];
            $this->name = $data['name'];
            $this->take_time = Tool::handelString($data['take_time']);
            $this->remark = isset($data['remark']) ? $data['remark'] : '';
            $this->status = isset($data['status']) ? $data['status'] : 1;
            $this->meal_type = isset($data['meal_type']) ? $data['meal_type'] : 1;
            $res = $this->save();
        }
        return $res;
    }

    /***
     * @param $param
     * @return array
     * 获取健康提醒
     */
    public function getHealthReminder($param)
    {
        if(isset($param['case_id'])){
            $or[] =['user_id','=',$param['user_id']];
            $or[] =['case_id', '=', 0];
            $list = $this->where([['case_id', '=', $param['case_id']]])->orWhere($or)->orderByDesc("id")->get();
        }elseif (isset($param['user_id'])){
            $list = $this->where([['user_id', '=', $param['user_id']]])->orderByDesc("id")->get();
        }
        if (!empty($list)) {
            foreach($list as $k=>&$v){
                $tim = $this->setResidueTime($v);
                $v->pass = $tim['pass'];
                $v->still = $tim['still'];
                $v->user_types = $this->getUseTypes($v);
                $v->frequency_dec = $this->getFrequencyDec($v);
            }
            return $list->toArray();
        }
        return [];
    }

    /**
     * @param $data
     * 计算剩余多少条和已过多少天
     */
    public function setResidueTime($data)
    {
        $today = time();
        $all = round((strtotime($data['end_time']." 23:59:59") - strtotime($data['start_time'] ." 00:00:00")) / 86400);
        if($today < strtotime($data['end_time']." 23:59:59")){
           $pass = round(($today - strtotime($data['start_time'] ." 00:00:00")) / 86400);
        }else{
            $pass = $all;
        }
        if ($pass <= 0) {
            $pass = 0;
        }
        $still = ($all - $pass);
        if ($still >= $all) {
            $still = $all;
        }elseif ($still < 0){
            $still = 0;
        }
        $data['pass'] = $pass;
        $data['still'] = $still;
        return $data;
    }

    /***
     * @param $data
     * @return mixed
     * 获取删除用药提醒
     */
    public function getInfo($id)
    {
        $res =  $this->where([['id', '=', $id]])->first();
        $res->user_types = $this->getUseTypes($res);
        $res->frequency_dec = $this->getFrequencyDec($res);
        return $res;
    }
//    编辑使用
    public function getEditor($id)
    {
        return  $this->where([['id', '=', $id]])->first();
    }

    /**
     * @param $filter
     * @return int
     * 删除用药提醒
     */
    public function del($filter)
    {
        return $this::destroy($filter['id']);
    }
}
